import Koa from 'koa';
import KoaBody from 'koa-body';
import cors from 'koa2-cors';
import fs from 'fs';
import path from 'path';
import routers from './router';

const app = new Koa();
app.use(KoaBody({ multipart: true, formLimit: '20mb' }));
app.use(cors());
if (fs.existsSync(path.resolve() + '/assets/token.json')) {
    const userStr = fs.readFileSync(path.resolve() + '/assets/token.json', 'utf-8');
    const user = JSON.parse(userStr);
    global.userToken = user.token;
    global.userOpenId = user.openId;
}

//加载路由
app.use(routers.routes()).use(routers.allowedMethods());

const port = process.env.PORT || '8082';
app.listen(port, function () {
    console.log(`服务器运行在http://127.0.0.1:${port}`);
    // RefreshNginx();
});
